package com.irishud.hudnavi;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.hud.base.protocol.Endpoint;
import com.irishud.protocol.BrightnessParam;
import com.irishud.protocol.DeviceSetting;
import com.irishud.protocol.MotorControlParam;
import com.irishud.protocol.NaviOnBoardModeParam;
import com.irishud.protocol.NaviParams;
import com.irishud.protocol.NaviUpdateInfo;
import com.irishud.protocol.ProtoMsg;
import com.irishud.protocol.TrafficStatusUpdateData;

import java.io.FileWriter;
import java.io.IOException;

import de.greenrobot.event.EventBus;


/**
 * 协议数据解析
 * Created by lucian on 16-12-12.
 */

public class ProtocolMessageDispatcher {
    EventBus eventBus = EventBus.getDefault();
    String TAG = "ProtoMessageDispatcher";
    static ProtocolMessageDispatcher instance = new ProtocolMessageDispatcher();
    public static ProtocolMessageDispatcher getInstance(){
        return instance;
    }
    // 处理通信协议
    public void handleProtoMsg(Endpoint endpoint,String action, byte[] jsonBytes){
        String content = new String(jsonBytes,0,jsonBytes.length) ;

        if(action.equals("updateNaviSpeed")){
            final NaviUpdateInfo updateInfo = JSON.parseObject(content,NaviUpdateInfo.class);
            // NaviSpeed
            eventBus.post(updateInfo);
        }

        if(action.equals("updateNaviInfo")){
            final NaviUpdateInfo updateInfo = JSON.parseObject(content,NaviUpdateInfo.class);
            // TODO 更新当前
            eventBus.post(updateInfo);
        }

        if(action.equals("switchToWifiMode")){
        }

        // 调整屏幕亮度
        if(action.equals("adjust_light")){
            final BrightnessParam brightness = JSON.parseObject(content,BrightnessParam.class);
            int value = brightness.value;
            Log.i(TAG,"adjust brightness value="+value);
            eventBus.post(brightness);
        }

        if(action.equals("set_navi_on_board")){
            NaviOnBoardModeParam how = JSON.parseObject(content,NaviOnBoardModeParam.class);
        }

        if(action.equals("start_on_board_navi")){
            NaviParams naviParams = JSON.parseObject(content,NaviParams.class);
            Log.i(TAG," ---------------start on board navi ---------> 车载导航模式! protoMsg.content:"+content);
            NaviAdapter.InitInstance().startNaviFrom(naviParams);
        }

        if(action.equals("stop_on_board_navi")){
            Log.i(TAG," ---------------stop on board navi ---------> 车载导航模式!");
            NaviAdapter.InitInstance().Close();
        }

        if(action.equals("get_device_settings")){// 查询设备设置信息
            DeviceSetting deviceSetting = new DeviceSetting();
            ProtoMsg response = new ProtoMsg("device.settings",JSON.toJSONString(deviceSetting));
            endpoint.send(response.toBytes());
        }

        if(action.equals("set_motor1_rotate") || action.equals("set_motor2_rotate")){
            MotorControlParam motorControlParam = JSON.parseObject(content,MotorControlParam.class);
            Log.e(TAG,"----->马达控制："+action + " param:"+motorControlParam.toString());
            try {
                FileWriter fileWriter = new FileWriter("/sys/bus/platform/drivers/commom2phase/rwreg");
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(motorControlParam.direction);
                stringBuilder.append(" ");
                stringBuilder.append(motorControlParam.motorId);
                fileWriter.write(stringBuilder.toString());
                fileWriter.close();
            } catch (IOException e) {
                Log.e(TAG,"error motor control:"+e.getMessage());
            }
        }

        if(action.equals("updateTrafficStatus")){
            TrafficStatusUpdateData trafficStatusUpdateData = JSON.parseObject(content,TrafficStatusUpdateData.class);
            eventBus.post(trafficStatusUpdateData);
        }

        if(action.equals("x3_open_demo_mode")){
            eventBus.post(new x3obdDemoEvent(x3obdDemoEvent.Open));
        }

        if(action.equals("x3_close_demo_mode")){
            eventBus.post(new x3obdDemoEvent(x3obdDemoEvent.Close));
        }

        if(action.equals("x3_demo_go_prev")){
            eventBus.post(new x3obdDemoEvent(x3obdDemoEvent.GoPrev));
        }

        if(action.equals("x3_demo_go_next")){
            eventBus.post(new x3obdDemoEvent(x3obdDemoEvent.GoNext));
        }
    }
}
